package com.sportschain.app.core.activity;

import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sportschain.app.R;
import com.sportschain.app.core.base.BaseActivity;
import com.sportschain.app.core.widget.ViewPagerIndicator;

import butterknife.BindView;
import butterknife.OnClick;

public class SplashActivity extends BaseActivity {


    @BindView(R.id.splash_vp)
    ViewPager mSplashVp;
    @BindView(R.id.splash_vp_indicator)
    ViewPagerIndicator mSplashVpIndicator;
    @BindView(R.id.start_btn)
    TextView mStartBtn;

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
        mSplashVpIndicator.attachViewPager(mSplashVp);
        mSplashVpIndicator.setIndicatorColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));
    }

    @Override
    public void initData() {
        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                View view = null;
                switch (position) {
                    case 0:
                        view = View.inflate(mActivity, R.layout.item_splash1, null);
                        break;
                    case 1:
                        view = View.inflate(mActivity, R.layout.item_splash2, null);
                        break;
                    case 2:
                        view = View.inflate(mActivity, R.layout.item_splash3, null);
                        break;
                }
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        };

        mSplashVp.setOffscreenPageLimit(2);
        mSplashVp.setAdapter(pagerAdapter);
    }

    @Override
    public void initListener() {
        mSplashVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == 2 && mStartBtn.getVisibility() != View.VISIBLE) {
                    mStartBtn.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @OnClick(R.id.start_btn)
    public void onClick() {
        finish();
    }
}
